<template>
  <div class="myDialog">
    <!-- 弹窗组件 -->
    <el-dialog
      :visible.sync="dialogFormVisible"
      :title="title"
      :before-close="handleClose"
      :close-on-click-modal="false"
      width="600px"
    >
      <dynForm
        ref="qualifyForm"
        :info-form="form"
        :search-tags="searchTags"
        :rules="rules"
        :is-inline="false"
        label-width="160px"/>
      <span slot="footer">
        <el-button icon="el-icon-circle-close" @click="handleClose">{{
          $t("cancel")
        }}</el-button>
        <el-button
          v-btnThrottle="doSave"

          type="primary"
          icon="el-icon-receiving"
        >{{ $t('save') }}</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import toolingquanlifyApi from '@/api/Tooling/quanlify'
import mySetting from '@/mixins/mySetting'
import { betterTips } from '@/utils/notification'
export default {
  name: 'QualifyForm',
  mixins: [mySetting],
  data() {
    return {
      form: {},
      title: '',
      dialogFormVisible: false,
      searchTags: [
        { label: this.$t('testerId'), prop: 'eqp2Id', tag: 'DynInput' },
        { label: this.$t('handlerId'), prop: 'eqp1Id', tag: 'DynInput' },
        { label: 'Device Process', prop: 'deviceProcess', tag: 'DynInput' },
        { label: 'Test Flow', prop: 'testFlow', tag: 'DynInput' },
        { label: 'DSA ID', prop: 'dsaId', tag: 'DynInput' },
        { label: 'COK ID', prop: 'cokId', tag: 'DynInput' },
        { label: 'MP ID', prop: 'mpId', tag: 'DynInput' },
        { label: 'Handler Recipe', prop: 'recipe', tag: 'DynInput' },
        { label: 'Interface Recipe', prop: 'interfaceRecipe', tag: 'DynInput' },
        { label: 'C-Tray Parts Number', prop: 'partNo', tag: 'DynInput' }
      ],
      rules: {
        eqp2Id: [{ required: true, message: `${this.$t('pleaseSelect')}${this.$t('testerId')}`, trigger: 'blur' }],
        eqp1Id: [{ required: true, message: `${this.$t('pleaseSelect')}${this.$t('handlerId')}`, trigger: 'blur' }],
        deviceProcess: [{ required: true, message: `${this.$t('pleaseSelect')}Device Process`, trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 新增数据
    doSave() {
      this.$refs.qualifyForm.$refs.dynForm.validate((valid) => {
        if (!valid) {
          return false
        }
        if (this.form.id) {
          // 存在id则是更新
          toolingquanlifyApi.edit({ id: this.form.id, ...this.form }).then((res) => {
            betterTips(res, this.handleClose, this.handleUpdate)
          })
        } else {
          // 不存在id则是新增
          toolingquanlifyApi.add(this.form).then((res) => {
            betterTips(res, this.handleClose, this.handleUpdate)
          })
        }
      })
    },
    // 打开弹窗
    openDialog(row) {
      if (row && row.id) {
        toolingquanlifyApi.get({ id: row.id }).then((res) => {
          this.form = res.data
          this.title = this.$t('modify')
        })
      } else {
        this.title = this.$t('add')
      }
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs.qualifyForm.$refs.dynForm.clearValidate()
      })
    },
    // 关闭弹窗
    handleClose() {
      Object.assign(this.$data, this.$options.data.call(this))
    },
    handleUpdate() {
      this.$emit('initList')
    }
  }
}
</script>
